
import {queryRoom,queryServiceLifeDetail,getCabinetCountList} from '../../services/ledger';

var dispatchG = undefined;
export default {
  namespace: 'cabinetCount',
  state: {
    departList: [],
    departLoading:false,
    departColumnsList: [],
    departWidth:500,
    chartData:[],
    roomList:[],
    departments:[],
    detailList:[],
    modalVisible:false,
    pageNum:1,
    pageSize:10,
    total:0,
    brandId:'',
    roomId:'',
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/ledger/cabinetCount') {
          dispatch({ type: 'getCabinetCountList',payload:{}});
          dispatch({ type: 'queryRoom' });
          dispatchG=dispatch;
        }
      });
    },
  },
  effects: {
    *getCabinetCountList({ payload },{ call, put }) {
      yield put({ type: 'setState', payload: {departLoading:true} });
      const { body }  = yield call(getCabinetCountList,payload);
      // if(body.data.columns!=null&&body.data.columns.length>0){
      //   var onViewItem=function(id,roomId){
      //     dispatchG({
      //       type: 'showDataDetial',
      //       payload: {id:id,roomId:roomId,pageSize:10,pageNum:1},
      //     })
      //   }
      //   body.data.columns.map((d)=>{
      //     if(d.title!='机房/使用年限'){
      //       d.render=function(value, row, index){
      //           return (
      //               <span>
      //                   <a onClick={onViewItem.bind(null, d.key,row.roomId)}>{value}</a>
      //               </span>
      //           )
      //         }
      //     }
      //   });
      // }
      yield put({
        type: 'setState',
        payload: {
          departList:body.data.dataList,
          departLoading:false,
          chartData:body.data.pieData,
        }
      });
    },
   
    *queryRoom({},{ call, put }) {
      const { body }  = yield call(queryRoom);
      yield put({
        type: 'setState',
        payload: {
          roomList:body.data,
        }
      });
    },
    *showDataDetial({payload},{ call, put }){
      const { body }  = yield call(queryServiceLifeDetail,payload);
      yield put({
        type: 'setState',
        payload: {
          detailList:body.data,
          modalVisible:true,
          pageNum:body.current,
          pageSize:body.size,
          total:body.iTotalRecords,
          brandId:payload.id,
          roomId:payload.roomId,
        }
      });
    }
  },

  reducers: {
    setState(state,action){
      return { ...state, ...action.payload };
    },
    hideModal(state,action){
      return { ...state, ...action.payload,modalVisible:false,
        pageNum:1,pageSize:10,total:0};
    }
  },
};
